Enable completion for both bash and zsh#1514
Draft
janweinschenker wants to merge 4 commits intoapache:masterfrom
Draft
Enable completion for both bash and zsh#1514janweinschenker wants to merge 4 commits intoapache:masterfrom
janweinschenker wants to merge 4 commits intoapache:masterfrom
Conversation
Author
|
Hi all, please let me know if anything is missing or needs to be changed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes from Bash to Zsh Completion Script
This document outlines the changes made to adapt
mvnd-bash-completion.bashfor zsh compatibility inmvnd-bash+zsh-completion.sh. These changes enable the same completion script to work in both bash and zsh shells.Overview
The zsh version of the completion script maintains backward compatibility with bash while adding zsh-specific enhancements. The script auto-detects the shell type and applies the appropriate behavior.
Key Changes
1. Shell Detection
A new shell detection mechanism was added at the beginning of the script:
This allows the script to conditionally execute shell-specific code paths.
2. Zsh Completion System Initialization
A new block initializes the zsh completion system and enables bash compatibility mode:
Key components:
compinit- Initializes zsh's completion systembashcompinit- Enables bash-style completion functions (complete,compgen,COMPREPLY)setopt- Sets zsh options for improved completion behaviorCOMP_WORDBREAKS- Defines word break characters (bash sets this automatically)3. Function Existence Check
The
function_existsfunction was modified to handle both shells:declare -F $1 > /dev/nulltypeset -f $1 > /dev/null 2>&1Zsh uses
typesetinstead ofdeclarefor function introspection. The2>&1redirect suppresses potential error messages.4. Variable Listing for Plugin Discovery
The
common_pluginsvariable discovery was updated:compgen -v | grep "^plugin_goals_.*"typeset + | grep "^plugin_goals_"compgen -vto list all variable namestypeset +to list variable names (the+modifier shows only names)5. Indirect Variable Expansion
When accessing plugin goals dynamically by variable name, different syntax is required:
${!var_name}${(P)var_name}${!var_name}for indirect expansion (dereference)${(P)var_name}- the(P)flag enables parameter name expansionThe updated code:
Summary of Shell Differences
declare -F nametypeset -f namecompgen -vtypeset +${!varname}${(P)varname}autoload -Uz compinit && compinitautoload -Uz bashcompinit && bashcompinitCOMP_WORDBREAKSUsage
The script works automatically in both shells. Simply source it in your shell configuration: